/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package captchabuster;

import com.googlecode.fannj.Fann;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author alexnewski
 */
public class Test {

    public Test() {
        this.truepositive = 0;
        this.falsepositive = 0;
        this.truenegative = 0;
        this.falsenegative = 0;
        this.total = 0;
        this.correct = 0;
        this.incorrect = 0;
        this.percentage = 0.0f;
        this.testlist = new ArrayList<float[]>();
        this.preditedanswer = new ArrayList<Predicted>();
    }
    protected int truepositive;
    protected int falsepositive;
    protected int truenegative;
    protected int falsenegative;
    protected int total;
    protected int correct;
    protected int incorrect;
    protected float percentage;
    List<float[]> testlist;
    List<Predicted> preditedanswer;

    /**
     * Get the value of incorrect
     *
     * @return the value of incorrect
     */
    public int getIncorrect() {
        return incorrect;
    }

    /**
     * Set the value of incorrect
     *
     * @param incorrect new value of incorrect
     */
    public void setIncorrect(int incorrect) {
        this.incorrect = incorrect;
    }

    /**
     * Get the value of correct
     *
     * @return the value of correct
     */
    public int getCorrect() {
        return correct;
    }

    /**
     * Set the value of correct
     *
     * @param correct new value of correct
     */
    public void setCorrect(int correct) {
        this.correct = correct;
    }

    /**
     * Get the value of total
     *
     * @return the value of total
     */
    public int getTotal() {
        return total;
    }

    /**
     * Set the value of total
     *
     * @param total new value of total
     */
    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * Get the value of truenegative
     *
     * @return the value of truenegative
     */
    public int getTruenegative() {
        return truenegative;
    }

    /**
     * Set the value of truenegative
     *
     * @param truenegative new value of truenegative
     */
    public void setTruenegative(int truenegative) {
        this.truenegative = truenegative;
    }

    /**
     * Get the value of falsenegative
     *
     * @return the value of falsenegative
     */
    public int getFalsenegative() {
        return falsenegative;
    }

    /**
     * Set the value of falsenegative
     *
     * @param falsenegative new value of falsenegative
     */
    public void setFalsenegative(int falsenegative) {
        this.falsenegative = falsenegative;
    }

    /**
     * Get the value of falsepositive
     *
     * @return the value of falsepositive
     */
    public int getFalsepositive() {
        return falsepositive;
    }

    /**
     * Set the value of falsepositive
     *
     * @param falsepositive new value of falsepositive
     */
    public void setFalsepositive(int falsepositive) {
        this.falsepositive = falsepositive;
    }

    /**
     * Get the value of truepositive
     *
     * @return the value of truepositive
     */
    public int getTruepositive() {
        return truepositive;
    }

    /**
     * Set the value of truepositive
     *
     * @param truepositive new value of truepositive
     */
    public void setTruepositive(int truepositive) {
        this.truepositive = truepositive;
    }

    public float getPercentage() {
        return percentage;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    public void run(String letter) throws FileNotFoundException, IOException {

        String netpath = "networks/" + letter + ".net";
        File net = new File(netpath);
        String testpath = "test_data/" + letter + ".data";
        File test = new File(testpath);

        Fann fann = new Fann(net.getAbsolutePath());
        Read r = new Read(test.getAbsolutePath());
        List<float[]> list = r.processLineByLine();

        for (int i = 1; i < list.size(); i = i + 2) {
            float[] inputs = list.get(i);
            float[] outputs = fann.run(inputs);
            total++;
            for (int j = 0; j < outputs.length; j++) {
                if (outputs[j] > 0.5) {
                    if (list.get(i + 1)[0] == Float.parseFloat("1.0")) {
                        //System.out.println("--TRUE POSITIVE");
                        correct++;
                        truepositive++;
                    } else {
                        //System.out.println("FALSE POSITIVE");
                        incorrect++;
                        falsepositive++;
                    }
                } else {

                    if (list.get(i + 1)[0] == Float.parseFloat("0.0")) {
                        //System.out.println("--TRUE NEGATIVE");
                        correct++;
                        truenegative++;
                    } else {
                        //System.out.println("FALSE NEGATIVE");
                        incorrect++;
                        falsenegative++;
                    }
                }
            }

        }

        percentage = (float) correct / total;
        fann.close();
    }

    public void run(String symbol, String network, String typenet, String testdir) throws FileNotFoundException, IOException {

        String netpath = "networks/" + network + typenet + ".net";
        File net = new File(netpath);
        String testpath = testdir + "/" + symbol + ".data";
        File test = new File(testpath);

        Fann fann = new Fann(net.getAbsolutePath());
        Read r = new Read(test.getAbsolutePath());
        if (this.testlist.isEmpty()) {
            this.testlist = r.processLineByLine();
        }
        int count = 0;
        boolean first = false;
        for (int i = 1; i < testlist.size(); i = i + 2) {
            float[] inputs = testlist.get(i);
            if (testlist.get(i + 1)[0] == Float.parseFloat("1.0")) {
                float[] outputs = fann.run(inputs);
                if (this.preditedanswer.isEmpty() || first) {
                    first = true;
                    Predicted predicted = new Predicted(symbol, outputs[0]);
                    this.preditedanswer.add(predicted);
                } else {
                    Predicted predicted = this.preditedanswer.get(count);
                    if (outputs[0] > predicted.getMax()) {
                        predicted.setMax(outputs[0]);
                        predicted.setLetter(network);
                    }
                    count++;
                }
            }

        }
        fann.close();
    }

    public void accuracy(String symbol) {
        float p = 0.0f;
        int count = 0;
        for (Predicted predicted : preditedanswer) {
            if (predicted.letter.matches(symbol)) {
                count++;
            }
        }
        p = (float) count / (float) preditedanswer.size();
        System.out.println(symbol + " has accuracy: " + p);

    }
}
